package cn.lsp.appsys.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.jws.WebParam.Mode;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import cn.lsp.appsys.pojo.app_category;
import cn.lsp.appsys.pojo.app_info;
import cn.lsp.appsys.pojo.app_version;
import cn.lsp.appsys.pojo.backendUser;
import cn.lsp.appsys.pojo.dataDictionary;
import cn.lsp.appsys.service.BackendUserService;
import cn.lsp.appsys.service.DevUserService;
import cn.lsp.appsys.tools.PageSupport;

@Controller
@RequestMapping("/Backend")
public class BackendUserController {

	private Logger logger = Logger.getLogger(BackendUserController.class);
	
	@Resource
	private BackendUserService backendUserService;
	
	@Resource
	private DevUserService devUserService;
	
	//后台用户登录页面
	@RequestMapping(value="/Login",method=RequestMethod.GET)
	public String Login(){
		return "jsp/backendlogin";
	}
	
	//验证登录
	@RequestMapping(value="/goLogin",method=RequestMethod.POST)
	public String goLogin(@RequestParam String userCode,@RequestParam String userPassword,HttpSession session,Model model,HttpServletRequest request){
		logger.debug("========>进入后台登录验证：" + userCode + "," + userPassword);
		backendUser bkUser = backendUserService.Login(userCode, userPassword);
		if(null != bkUser){
			session.setAttribute("userSession", bkUser);
			logger.debug("==========>后台用户登录成功!");
			return "jsp/backend/main";
		}
		logger.debug("==========>后台用户登录失败!");
		request.setAttribute("error", "用户名或密码错误!");
		return "jsp/backendlogin";
	}
	
	//注销开发者页面
	@RequestMapping(value="/logout",method=RequestMethod.GET)
	public String Logout(HttpSession session){
		session.removeAttribute("userSession");
		return "redirect:/Backend/Login";
	}
	
	//ajax联动分类下拉框
	@RequestMapping(value="categorylevellist.json",method=RequestMethod.GET)
	@ResponseBody
	public Object Classification(@RequestParam String pid){
		logger.debug("===========联动分类 pid="+pid);
		List<app_category> categoriesList = null;
		if(pid != null && pid != ""){
			categoriesList = backendUserService.MainClassification(Integer.valueOf(pid));
		}else{
			categoriesList = backendUserService.OneClassification();
		}
		return JSON.toJSONString(categoriesList);
	}
	
	//APP审核页面
	@RequestMapping(value="/bkUserList")
	public String bkUserList(Model model,
			@RequestParam(value="querySoftwareName",required=false)String querySoftwareName,
			@RequestParam(value="queryFlatformId",required=false)Integer queryFlatformId,
			@RequestParam(value="queryCategoryLevel1",required=false)Integer queryCategoryLevel1,
			@RequestParam(value="queryCategoryLevel2",required=false)Integer queryCategoryLevel2,
			@RequestParam(value="queryCategoryLevel3",required=false)Integer queryCategoryLevel3,
			@RequestParam(value="pageIndex",required=false)String pageIndex){
		logger.debug("=========<>软件名称：" + querySoftwareName);
		logger.debug("=========<>所属平台：" + queryFlatformId);
		logger.debug("=========<>一级分类：" + queryCategoryLevel1);
		logger.debug("=========<>二级分类：" + queryCategoryLevel2);
		logger.debug("=========<>三级分类：" + queryCategoryLevel3);
		
		List<dataDictionary> FlatformName = backendUserService.getFlatformName();		//所属平台
		List<app_category> One = backendUserService.OneClassification();	//一级分类
		//Integer _parentId = 0;
		List<app_category> Two = backendUserService.MainClassification(queryCategoryLevel1);	//二级分类
		List<app_category> Three = backendUserService.MainClassification(queryCategoryLevel2);	//三级分类
		
		
		//设置页面容量
    	int pageSize = 5;
    	//当前页码
    	int currentPageNo = 1;
    	if(pageIndex != null){
    		currentPageNo = Integer.valueOf(pageIndex);
    	}
    	if(querySoftwareName == null){
    		querySoftwareName = "";
    	}
		
    	//总数量
    	int count = backendUserService.getAppinfoCount(querySoftwareName, queryFlatformId, queryCategoryLevel1, queryCategoryLevel2, queryCategoryLevel3);
    	logger.debug("=========总数量：" + count);
    	
    	//总页数
    	PageSupport pages=new PageSupport();
    	//设置当前页
    	pages.setCurrentPageNo(currentPageNo);
    	//每页大小
    	pages.setPageSize(pageSize);
    	//传入总条数，算出总页数
    	pages.setTotalCount(count);
    	//获取总页数
    	int totalPageCount = pages.getTotalPageCount();
    	//控制首页和尾页
    	if(currentPageNo < 1){
    		currentPageNo = 1;
    	}else if(currentPageNo > totalPageCount){
    		currentPageNo = totalPageCount;
    	}
    	
    	List<app_info> infoList = backendUserService.getAppinfoList(querySoftwareName, queryFlatformId, queryCategoryLevel1, queryCategoryLevel2, queryCategoryLevel3, (currentPageNo-1)*pageSize, pageSize);
    	
		model.addAttribute("flatFormList", FlatformName);
		model.addAttribute("categoryLevel1List", One);
		model.addAttribute("categoryLevel2List", Two);
		model.addAttribute("categoryLevel3List", Three);
		
		model.addAttribute("querySoftwareName", querySoftwareName);
		model.addAttribute("queryFlatformId", queryFlatformId);
		model.addAttribute("queryCategoryLevel1", queryCategoryLevel1);
		model.addAttribute("queryCategoryLevel2", queryCategoryLevel2);
		model.addAttribute("queryCategoryLevel3", queryCategoryLevel3);
		
		model.addAttribute("appInfoList", infoList);
		model.addAttribute("pages", pages);
		return "jsp/backend/applist";
	}
	
	//审核页面
	@RequestMapping(value="/appinfocheck")
	public String AppinfoCheck(@RequestParam Integer aid,@RequestParam Integer vid,
			Model model){
		app_info info = devUserService.infoview(aid);		//APP基础信息
		app_version version = backendUserService.getAppversionByappId(aid);		//最新版本信息
		
		model.addAttribute("appVersion", version);
		model.addAttribute("appInfo", info);
		return "jsp/backend/appcheck";
	}
	
	//审核
	@RequestMapping(value="/appinfochecksave")
	public String AppinfoCheckSave(@RequestParam Integer status,@RequestParam Integer id){
		logger.debug("========>审核状态：" + status);
		if(status == 2){
			if(backendUserService.AppinfoModifyStatus(id, status) > 0){
				logger.debug("========>审核通过执行成功！");
				return "redirect:/Backend/bkUserList";
			}else {
				logger.debug("========>审核通过执行失败！");
				return "redirect:/Backend/appinfocheck";
			}
		}else if(status == 3){
			if(backendUserService.AppinfoModifyStatus(id, status) > 0){
				logger.debug("========>审核不通过执行成功！");
				return "redirect:/Backend/bkUserList";
			}else {
				logger.debug("========>审核不通过执行失败！");
				return "redirect:/Backend/appinfocheck";
			}
		}
		return "";
	}
	
}
